package com.piz.service.userservice;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.piz.dao.common.CityDao;
import com.piz.dao.user.RoleDao;
import com.piz.dao.user.UserDao;
import com.piz.model.City;
import com.piz.model.Role;
import com.piz.model.State;
import com.piz.model.User;
import com.piz.service.common.UnexpectedServiceException;

public class UserService implements UserDetailsService , ApplicationContextAware{
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private CityDao cityDao;
	@Autowired
	private RoleDao roleDao;
	ApplicationContext applicationContext;
	String str;
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		
		try {
			User user = userDao.findUserByUserName(username);
			if(user == null)
			{
				return null;
			}
			Authentication request = new UsernamePasswordAuthenticationToken(username, user.getPassword());
			
			SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken(request.getName(),
					request.getCredentials(), user.getAuthorities()));
		
		
			return user;
		} catch (UsernameNotFoundException e) {
			throw new UsernameNotFoundException("user not found with username : "+username,e);
		}
		
	}
	
	public boolean isUsernameExsist(String username)
	{
		try {
			User user = userDao.findUserByUserName(username);
			if(user != null)
				return true;
			else
				return false;
			
		} catch (Exception e) {
			throw new UnexpectedServiceException("exception in finding user :" + username,e);
		}
		
	}
	
	public long createUser(User user)
	{
		try {
			Set<Role> roles = new HashSet<Role>();
			roles.add(roleDao.getRoleByName("cutomer").get(0)); 
			user.setRoles(roles);
			Long userId = userDao.saveUser(user);
			return userId;
		} catch (Exception e) {
			throw new UnexpectedServiceException("exception in creating user : "+user.getUsername(),e);
			
		}
		
	 }
	
	public List<City> findCitiesInState(Long state)
	{
		try {
			return cityDao.getCityInState(state);
		} catch (Exception e) {
			throw new UnexpectedServiceException("exception in fetching cities in state: "+state,e);
		}
	}
	
	public List<State> getAllStates()
	{
		return cityDao.getallStates();
	}
	
	public User findUserByUsername(String username)
	{
		try {
			User user = userDao.findUserByUserName(username);
			return user;
		} 
		catch(Exception e)
		{
			throw new UnexpectedServiceException("Exception in fetching user : "+username,e);
		}
	}



	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = applicationContext;
	}
	
	
}
